home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 7 / FM Towns Free Software Collection 7.iso / ms_dos / dmove86 / dmsort.c < prev    next >
Text File  |  1993-11-30  |  3KB  |  175 lines

  1. /*
  2.  
  3. dmsort.c --- ソート処理ルーチン
  4.  
  5. Copyright (c) 1993 Delmonta
  6.  
  7. */
  8.  
  9. #include<stdio.h>
  10. #include<stdlib.h>
  11. #include<farstr.h>
  12. #include<dos.h>
  13. #include<assert.h>
  14. #include"dmove86.h"
  15.  
  16. static    int    cmpfname(p,q)
  17. struct DIRENTRY    far **p,far **q;
  18. {
  19.     return    far_strncmp((*p)->filename,(*q)->filename,11);
  20. }
  21.  
  22. static    int    cmpftime(p,q)
  23. struct DIRENTRY    far **p,far **q;
  24. {
  25.     register unsigned short    a,b;
  26.  
  27.     a = 
  28.     a = (*p)->date;
  29.     b = (*q)->date;
  30.  
  31.     if    (a!=b)
  32.         return a-b;
  33.  
  34.     a = (*p)->time;
  35.     b = (*q)->time;
  36.  
  37.     return a-b;
  38. }
  39.  
  40. static    int    cmpextname(p,q)
  41. struct DIRENTRY    far **p,far **q;
  42. {
  43.     register int    a;
  44.  
  45.     a = far_strncmp((*p)->extname,(*q)->extname,3);
  46.     if    (a)
  47.         return a;
  48.     else
  49.         return    far_strncmp((*p)->filename,(*q)->filename,8);
  50. }
  51.  
  52. static    int    cmpattr(p,q)
  53. struct DIRENTRY    far **p,far **q;
  54. {
  55.     register unsigned char    a,b,x,y;
  56.  
  57.     a = (*p)->attr;
  58.     b = (*q)->attr;
  59.     x = y = 0;
  60.                     /* (1)ボリュームID    */
  61.     if    (a & _A_VOLID)        x += 0x40;
  62.     if    (b & _A_VOLID)        y += 0x40;
  63.                     /* (2)システムファイル    */
  64.     if    (a & _A_SYSTEM)        x += 0x20;
  65.     if    (b & _A_SYSTEM)        y += 0x20;
  66.                     /* (3)サブディレクトリ    */
  67.     if    (a & _A_SUBDIR)        x += 0x10;
  68.     if    (b & _A_SUBDIR)        y += 0x10;
  69.  
  70.     return y-x;
  71. }
  72.  
  73. static    int    Sortmode;    /* ソート基準(ファイル名/拡張子/日時/属性)    */
  74. static    int    Sorttype;    /* 昇順か降順か                */
  75.  
  76. #define    SORT_FILENAME    0
  77. #define    SORT_EXTNAME    1
  78. #define    SORT_TIMESTAMP    2
  79. #define    SORT_ATTR    3
  80.  
  81. #define    SORT_UP        0
  82. #define    SORT_DOWN    1
  83.  
  84. static    int    cmpdirentry(p,q)
  85. struct DIRENTRY    far **p,far **q;
  86. {
  87.     register int    a,b;
  88.  
  89.     a = (*p)->filename[0] - 0xe5;    /* 例外処理:未使用エントリーは    */
  90.     b = (*q)->filename[0] - 0xe5;    /* 無条件で後ろに回す        */
  91.  
  92.     if    (a==0 && b==0)    return 0;
  93.     else if    (a!=0 && b==0)    return -1;
  94.     else if    (a==0 && b!=0)    return 1;
  95.  
  96.     a = (*p)->filename[0] - '.';    /* 例外処理:"."、".."は        */
  97.     b = (*q)->filename[0] - '.';    /* 無条件で先頭へ        */
  98.  
  99.     if    (a==0 && b==0)    return 0;
  100.     else if    (a!=0 && b==0)    return 1;
  101.     else if    (a==0 && b!=0)    return -1;
  102.  
  103.  
  104.     switch(Sortmode)
  105.     {
  106.     case SORT_FILENAME:
  107.         a = cmpfname(p,q);
  108.         break;
  109.     case SORT_EXTNAME:
  110.         a = cmpextname(p,q);
  111.         break;
  112.     case SORT_TIMESTAMP:
  113.         a = cmpftime(p,q);
  114.         break;
  115.     case SORT_ATTR:
  116.         a = cmpattr(p,q);
  117.         break;
  118.     }
  119.  
  120.     if    (Sorttype == SORT_UP)
  121.         return a;
  122.     else
  123.         return -a;
  124. }
  125.  
  126. void    dmsort(dirtbl,s,e)
  127. struct    DIRENTRY far **dirtbl;
  128. unsigned int    s,e;
  129. {
  130. mode:;
  131.     switch(dm_errmes("ソート基準 F:ファイル名 E:拡張子 T:最終変更日時 A:属性"))
  132.     {
  133.     case 'F':
  134.     case 'f':
  135.         Sortmode = SORT_FILENAME;
  136.         break;
  137.     case 'E':
  138.     case 'e':
  139.         Sortmode = SORT_EXTNAME;
  140.         break;
  141.     case 'T':
  142.     case 't':
  143.         Sortmode = SORT_TIMESTAMP;
  144.         break;
  145.     case 'A':
  146.     case 'a':
  147.         Sortmode = SORT_ATTR;
  148.         break;
  149.     case '\033':
  150.         return;
  151.     default:
  152.         putchar('\7');
  153.         goto    mode;
  154.     }
  155.  
  156. type:
  157.     switch(dm_errmes("ソート順 U:昇順 D:降順"))
  158.     {
  159.     case 'u':
  160.     case 'U':
  161.         Sorttype = SORT_UP;
  162.         break;
  163.     case 'd':
  164.     case 'D':
  165.         Sorttype = SORT_DOWN;
  166.         break;
  167.     case '\033':
  168.         return;
  169.     default:
  170.         goto type;
  171.     }
  172.  
  173.     qsort(dirtbl+s, e-s+1, sizeof(struct DIRENTRY far *), cmpdirentry);
  174. }
  175.